package generics_and_collections.equals;

/**
 * Created with IntelliJ IDEA.
 * User: Sam
 * Date: 04/02/14
 * Time: 21:50
 * To change this template use File | Settings | File Templates.
 */

/**
 * If you do not have "Comparable<Person>", but just have interface "Comparable"
 * the compareTo() method will complain that you must have param as Object type.
 */
public class Person implements Comparable<Person>{

	public String name;
	public int age;
	public boolean isMale;

	public Person(String name, int age, boolean isMale) {
		this.name = name;
		this.age = age;
		this.isMale = isMale;
	}

	/**
	 * Used by Collections.sort(<List>).
	 * Can use whatever criteria we want (this case we use Strings inbuilt compareTo() and with 'name' property).
	 * Not using natural sort order, but age instead.
	 */
	public int compareTo(Person obj) {
		// do NOT cast, its risky, only cast in equals(), but in compareTo signature
		// you type it to the type you are expecting
		//Person other = (Person) obj;
		return this.name.compareTo(obj.name);
	}

	public String toString() {
		return name + " " + age + " " + isMale;
	}

	public boolean equals(Object obj) {
		if((obj instanceof Person) && ((Person)obj).age == age) {
			return true;
		}
		return false;
	}
}
